package main

import (
	"fmt"
	"go_code/chatroom/common/message"
	"go_code/chatroom/server/process"
	"go_code/chatroom/server/utils"
	"net"
)

type Processor struct {
	Conn net.Conn
}

//编写一个serverprocessMes函数
//根据客户端发送消息种类不同，决定调用哪个函数
func (this *Processor) ServerProcessMes(mes *message.Message)(err error)  {

	//接受群发消息
	fmt.Println("mes=", mes)

	switch mes.Type {
	case message.LoginMesType:
		up := &process.UserProcess{
			Conn: this.Conn,
		}
		//处理登陆
		err = up.ServerProcessLogin(mes)
	case message.RegisterMesType:
		//处理注册
		up := &process.UserProcess{
			Conn: this.Conn,
		}
		//处理登陆
		up.ServerProcessRegister(mes)
	case message.SmsMesType:
		//创建一个SmsMes的实例
		smsProcess := &process.SmsProcess{}
		smsProcess.SendGroupMes(mes)
	default:
		fmt.Println("消息类型不存在。。。")
	}
	return
}

func (this *Processor) Process2()(err error){
	//循环读客户端发送到信息
	for true {
		tf := &utils.Transfer{
			Conn: this.Conn,
		}

		//读取消息数据包，封装成函数
		mes, err := tf.ReadPkg()
		if err != nil {
			fmt.Println("readPkg(conn) err=", err)
			return err
		}
		//fmt.Println("message = ", mes)

		err = this.ServerProcessMes(&mes)
		if err != nil {
			fmt.Println("serverProcessMes err=", err)
			return err
		}
	}
	return
}
